package com.googlecode.project.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.connector.Request;

/**
 * Servlet implementation class for Servlet: DataDeletionServlet
 * 
 */
public class DataDeletionServlet extends javax.servlet.http.HttpServlet
		implements javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public DataDeletionServlet() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		
		res.setContentType("text/plain");
		PrintWriter out = res.getWriter();
		String requestNumber = req.getParameter("reqno");
		int parseNumber = Integer.parseInt(requestNumber);

		Connection con = null;
		try {
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
			con = DriverManager.getConnection("jdbc:derby:/DerbyDB/AssetDB");

			// Turn on transactions
			con.setAutoCommit(false);

			String inet = req.getRemoteAddr();
			Statement stmt = con.createStatement();
			String sql = "UPDATE REQUEST_DETAILS SET viewed = '1', checked_by = '" + inet + "' WHERE QUERY = ?";
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setInt(1, parseNumber);
			pst.executeUpdate();
			

			con.commit();
//			out.println("Removing query");
			String nextJSP = "/queryRemoved.jsp";
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
			dispatcher.forward(req,res);
		} catch (Exception e) {
			// Any error is grounds for rollback
			try {
				con.rollback();
			} catch (SQLException ignored) {
			}
			out.println("Failed");
		} finally {
			// Clean up.
			try {
				if (con != null)
					con.close();
			} catch (SQLException ignored) {
			}
		}
	}

}